package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import MessagePacks.MessagePack;
import MessagePacks.UpdatePack;
import MessagePacks.getCommitteePack;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ChairmanScreen extends MasterGUI {
	
	private static final long serialVersionUID = 1L;
	private JButton btnReadReport;
	private JButton btnTimeExtensionRequest;
	private MainGUI maingui;
	private JPanel panel_1;
	private JPanel panel;
	private JButton btnChooseTester;
	private JTextField textField;
	private JLabel lblCommitteeMemberId;
	private getCommitteePack gCommPck;
	private JButton btnDesignateTester;		
	private JPanel panel_2;
	private JComboBox<String> comboBox;
	private UpdatePack setTester;
	private UpdatePack updResOk;
	

	
	public ChairmanScreen(MainGUI maingui) {
		super(maingui);
		this.maingui=maingui;
		
		getCommitteeMem();
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(0, 40, 176, 201);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		setBtnReadReport();
		setBtnTimeExtensionRequest();
		setBtnChooseTester();

		
		panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblChairmanMenu = new JLabel("Chairman Menu");
		lblChairmanMenu.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblChairmanMenu);
		
		panel_2 = new JPanel();
		panel_2.setBounds(211, 110, 517, 50);
		panel_2.setVisible(false);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);
		
		lblCommitteeMemberId = new JLabel("Committee member  :");
		lblCommitteeMemberId.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblCommitteeMemberId.setBounds(10, 11, 174, 21);
		panel_2.add(lblCommitteeMemberId);
		
		setComboBox();
		
		setBtnDesignateTester();
		
		JLabel lblProcessId = new JLabel("Process ID : ");
		lblProcessId.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblProcessId.setBounds(211, 51, 106, 24);
		getContentPane().add(lblProcessId);
		
		textField = new JTextField();
		textField.setEditable(true);
		textField.setBounds(324, 55, 86, 20);
		textField.setText(Integer.toString(maingui.process.getprocessID()));
		textField.setEditable(false);
		getContentPane().add(textField);
		textField.setColumns(10);

	}

	public JButton getBtnReadReport() {
		return btnReadReport;
	}

	public void setBtnReadReport() {
		btnReadReport = new JButton("Read Report");
		btnReadReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"button Read Report pressed");
				close();
				EstimationReportDesicion ET_ReportDes = new EstimationReportDesicion(maingui);
				ET_ReportDes.setVisible(true);
			}
		});
		btnReadReport.setBounds(10, 11, 156, 23);
		panel_1.add(btnReadReport);
	}

	public JButton getBtnTimeExtensionRequest() {
		return btnTimeExtensionRequest;
	}

	public void setBtnTimeExtensionRequest() {
		btnTimeExtensionRequest = new JButton("Time Extension Request");
		btnTimeExtensionRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"button time extension pressed");
				close();
				TimeExtensionRequest TE = new TimeExtensionRequest(maingui);
				TE.setVisible(true);
			}
		});
		btnTimeExtensionRequest.setBounds(10, 45, 156, 23);
		panel_1.add(btnTimeExtensionRequest);
		

	}

	public JButton getBtnChooseTester() {
		return btnChooseTester;
	}

	public void setBtnChooseTester() {
		btnChooseTester = new JButton("Choose tester");
		if(maingui.process.getCurrentPhase()==4 && maingui.process.getResponsableOK()==0){
			btnChooseTester.setEnabled(true);
		}
		else{
			btnChooseTester.setEnabled(false);
		}
		
		btnChooseTester.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				JOptionPane.showMessageDialog(serverGUI.mainframe,"button Read Report pressed");
				panel_2.setVisible(true);
			}
		});
		btnChooseTester.setBounds(10, 79, 156, 23);
		panel_1.add(btnChooseTester);
	}
	
	public void getCommitteeMem(){
		gCommPck = new getCommitteePack();
		maingui.client.handleMessageFromClientUI(gCommPck);
		gCommPck = (getCommitteePack) (maingui.client.getMessage());
	}

	public JButton getBtnDesignateTester() {
		return btnDesignateTester;
	}

	public void setBtnDesignateTester() {
		btnDesignateTester = new JButton("designate tester");
		btnDesignateTester.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				updateResOk();
				setTesterforProc();
				
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Tester designated.");
				panel_2.setVisible(false);
				btnChooseTester.setEnabled(false);
			}
		});
		btnDesignateTester.setBounds(384, 12, 111, 23);
		panel_2.add(btnDesignateTester);
	}

	public JComboBox<String> getComboBox() {
		return comboBox;
	}

	public void setComboBox() {
		comboBox = new JComboBox<String>();
		comboBox.setBounds(194, 13, 101, 20);
		comboBox.addItem("");
		Iterator<Entry<String, String>> iter = gCommPck.getMyHashMap().entrySet().iterator();
		while (iter.hasNext()){
			Map.Entry mEntry = (Map.Entry) iter.next();
			if(maingui.process.getChairmanID()!=Integer.parseInt((String) mEntry.getKey())){
				comboBox.addItem((String) mEntry.getKey());
				}
		}
		panel_2.add(comboBox);
	}
	
	public void setTesterforProc(){
		setTester = new UpdatePack(MessagePack.setTester);
		setTester.setProcId(maingui.process.getprocessID());
		setTester.setWorkerId(Integer.parseInt((String) comboBox.getSelectedItem()));
		maingui.client.handleMessageFromClientUI(setTester);
	}
	public void updateResOk(){
		updResOk= new UpdatePack(MessagePack.updresponsible);
		updResOk.setProcId(maingui.process.getprocessID());
		updResOk.setResponOk(1);
		maingui.client.handleMessageFromClientUI(updResOk);
		
	}
}
